package com.project.dao.sys;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.project.common.Constants;
import com.project.common.Page;
import com.project.dao.BaseDao;
import com.project.entity.sys.Role;

/**
 * RoleDao
 * @author
 *
 */
@Repository
public class RoleDao extends BaseDao<Role>{

	/**
	 * 分页查询角色
	 * @param page
	 * @return
	 */
	public Page<Role> queryPage(Page<Role> page){
		String hql = "FROM Role t ";
		
		return super.queryPageForHql(page, hql);
	}
	
	/**
	 * 查询可用的角色
	 * @param status
	 * @return
	 */
	public List<Role> queryRolesByStatus(String status){
		String hql = "FROM Role t WHERE t.status = ? ";
		return super.find(hql, status);
	}
	
	/**
	 * 根据roleCode查询
	 * @param code
	 * @return
	 */
	public Role getByCode(String roleCode){
		String hql = "FROM Role t WHERE t.roleCode = ?";
		return super.get(hql, roleCode);
	}
	
	/**
	 * 根据roleName查询
	 * @param code
	 * @return
	 */
	public Role getByName(String roleName){
		String hql = "FROM Role t WHERE t.roleName = ?";
		return super.get(hql, roleName);
	}
	
	/**
	 * 根据id和name查询
	 * @param id
	 * @param roleName
	 * @return
	 */
	public Role getByIdAnName(Long id,String roleName){
		String hql = " FROM Role t WHERE t.id != ? AND roleName = ?";
		return super.get(hql,id,roleName);
	}
	
	public List<Map<String,Object>> queryList(String userCode){
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT r.id,r.role_code from s_user u  ")
			.append(" LEFT JOIN s_user_role ur on u.id = ur.USER_ID ")
			.append(" LEFT JOIN s_role r on ur.ROLE_ID = r.id ")
			.append(" 			AND r.status='").append(Constants.STATUS_OPEN).append("' ")
			.append(" WHERE u.user_code = '").append(userCode).append("' ");
			
		return super.findListBySql(sql.toString());	
		
	}
	
	
}
